package com.fanbai.score.common.Utils.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.fanbai.score.R;


public class PopupWindowUtils {
	private Context context;
	private View view;

	private PopupWindow popupWindow;
	private PopupWindowUtils popupWindowUtils;
	private static int screen_width;
	private static int screen_height;

	public PopupWindowUtils(Context context) {
		this.context = context;
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
		screen_width = dm.widthPixels;
		screen_height = dm.heightPixels;
		popupWindow = new PopupWindow(context);
		popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
		popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		this.popupWindowUtils = this;
	}

	public View getContentView() {
		return this.view;
	}

	public PopupWindowUtils setContentView(View view) {
		this.view = view;
		if (view != null) {
			popupWindow.setContentView(view);
			popupWindow.setWidth(screen_width);
			popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		} else {
			popupWindow.setContentView(new TextView(context));
			popupWindow.setWidth(screen_width);
			popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		return popupWindowUtils;
	}

	public PopupWindowUtils setContentView(View view, int width) {
		this.view = view;
		if (view != null) {
			popupWindow.setContentView(view);
			popupWindow.setWidth(width);
			popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		} else {
			popupWindow.setContentView(new TextView(context));
			popupWindow.setWidth(width);
			popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		return popupWindowUtils;
	}

	public PopupWindowUtils setContentView(View view, int width, int height) {
		this.view = view;
		if (view != null) {
			popupWindow.setContentView(view);
			popupWindow.setWidth(width);
			popupWindow.setHeight(height);
		} else {
			popupWindow.setContentView(new TextView(context));
			popupWindow.setWidth(width);
			popupWindow.setHeight(height);
		}
		return popupWindowUtils;
	}

	public PopupWindowUtils setWidth(int width) {
		popupWindow.setWidth(width);
		return popupWindowUtils;
	}

	public PopupWindowUtils setHeight(int height) {
		popupWindow.setHeight(height);
		return popupWindowUtils;
	}

	public PopupWindowUtils setWidthAndHeight(int width, int height) {
		popupWindow.setWidth(width);
		popupWindow.setHeight(height);
		return popupWindowUtils;
	}

	public void show(View parent, int gravity, int x, int y) {
		if (popupWindow != null) {
			ColorDrawable cd = new ColorDrawable(0x000000);
			popupWindow.setBackgroundDrawable(cd);
			popupWindow.setAnimationStyle(R.style.popwin_anim_style);
			WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
			lp.alpha = 0.8f;
			((Activity) context).getWindow().setAttributes(lp);
			popupWindow.update();
			popupWindow.setOnDismissListener(new OnDismissListener() {
				public void onDismiss() {
					WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
					lp.alpha = 1f;
					((Activity) context).getWindow().setAttributes(lp);
				}
			});
			popupWindow.showAtLocation(parent, gravity, x, y);
		}
	}

	/**
	 * 向下弹出菜单界面
	 * 
	 * @param anchor
	 *            出现在哪个view的下面
	 * @param xoff
	 * @param yoff
	 */
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		if (popupWindow != null) {
			ColorDrawable cd = new ColorDrawable(0x000000);
			popupWindow.setBackgroundDrawable(cd);
			popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
			WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
			lp.alpha = 0.9f;
			((Activity) context).getWindow().setAttributes(lp);
			popupWindow.update();
			popupWindow.setOnDismissListener(new OnDismissListener() {
				public void onDismiss() {
					WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
					lp.alpha = 1f;
					((Activity) context).getWindow().setAttributes(lp);
				}
			});
			popupWindow.showAsDropDown(anchor, xoff, yoff);
		}
	}

	public void dismiss() {
		if (popupWindow != null) {
			popupWindow.dismiss();
		}
	}

	public static int getScreenWidth() {
		return screen_width;
	}

	public static int getScreenHeight() {
		return screen_height;
	}

}
